﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BussinessLogic;
using BaseEntities;

namespace ATMSystem_G5_HaUI
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void btnInsertCard_Click(object sender, EventArgs e)
        {
            CheckCard();
        }

        public void CheckCard()
        {
            Card card = new Card();
            card.CardNo = txtCard.Text;
            if (Session["ReCardNo"] == null)
            {
                if (ValidationLogic.ValidCard(card) == true)
                {
                    UpdatePanelBlockCard.Visible = false;
                    UpdatePanelDefault.Visible = false;
                    UpdatePanelReValidPIN.Visible = false;
                    UpdatePanelValidCardFail.Visible = false;
                    UpdatePanelValidPIN.Visible = true;
                    UpdatePanelMainMenu.Visible = false;
                    Session["CardNo"] = txtCard.Text;
                }
                else
                {
                    UpdatePanelBlockCard.Visible = false;
                    UpdatePanelDefault.Visible = false;
                    UpdatePanelReValidPIN.Visible = false;
                    UpdatePanelValidCardFail.Visible = true;
                    UpdatePanelMainMenu.Visible = false;
                    UpdatePanelValidPIN.Visible = false;
                }
            }
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            Card card = new Card();
            if ((int)Session["Attempt"] < 3)
            {
                if (Session["RePIN"] == null)
                {
                    card.PIN = txtPIN.Text;
                    card.CardNo = Session["CardNo"].ToString();
                    if (ValidationLogic.ValidPIN(card) == true)
                    {
                        UpdatePanelBlockCard.Visible = false;
                        UpdatePanelDefault.Visible = false;
                        UpdatePanelReValidPIN.Visible = false;
                        UpdatePanelValidCardFail.Visible = false;
                        UpdatePanelMainMenu.Visible = true;
                        UpdatePanelValidPIN.Visible = false;
                        Session["Login"] = "true";
                    }
                    else
                    {
                        card.CardNo = Session["CardNo"].ToString();
                        UpdatePanelBlockCard.Visible = false;
                        UpdatePanelDefault.Visible = false;
                        UpdatePanelReValidPIN.Visible = true;
                        UpdatePanelValidCardFail.Visible = false;
                        UpdatePanelMainMenu.Visible = false;
                        UpdatePanelValidPIN.Visible = false;
                        Session["Attempt"] = (int)Session["Attempt"] + 1;
                        ValidationLogic.UpdateAttempt(card);
                        Session["RePIN"] = "true";
                    }
                }
                else
                {
                    card.CardNo = Session["CardNo"].ToString();
                    card.PIN = txtRePIN.Text;
                    if (ValidationLogic.ValidPIN(card) == true)
                    {
                        UpdatePanelBlockCard.Visible = false;
                        UpdatePanelDefault.Visible = false;
                        UpdatePanelReValidPIN.Visible = false;
                        UpdatePanelValidCardFail.Visible = false;
                        UpdatePanelMainMenu.Visible = true;
                        UpdatePanelValidPIN.Visible = false;
                        Session["Login"] = "true";
                    }
                    else
                    {
                        card.CardNo = Session["CardNo"].ToString();
                        UpdatePanelBlockCard.Visible = false;
                        UpdatePanelDefault.Visible = false;
                        UpdatePanelReValidPIN.Visible = true;
                        UpdatePanelValidCardFail.Visible = false;
                        UpdatePanelMainMenu.Visible = false;
                        UpdatePanelValidPIN.Visible = false;
                        ValidationLogic.UpdateAttempt(card);
                        Session["Attempt"] = (int)Session["Attempt"] + 1;
                    }
                }
            }
            else
            {
                card.CardNo = Session["CardNo"].ToString();
                UpdatePanelBlockCard.Visible = true;
                UpdatePanelDefault.Visible = false;
                UpdatePanelReValidPIN.Visible = false;
                UpdatePanelValidCardFail.Visible = false;
                UpdatePanelMainMenu.Visible = false;
                UpdatePanelValidPIN.Visible = false;
                ValidationLogic.UpdateStatus(card);
                ValidationLogic.ResetAttempt(card);
                Session["CardNo"] = null;
                Session["Attempt"] = 0;
                Session["RePIN"] = null;
            }
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            if (UpdatePanelMainMenu.Visible == true && PanelUC1.Visible == true)
            {
                UpdatePanelMainMenu.Visible = false;
                PanelUC1.Visible = false;
                PanelUC2.Visible = true;
            }
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            if (UpdatePanelMainMenu.Visible == true)
            {
                UpdatePanelMainMenu.Visible = false;
                PanelUC1.Visible = false;
                PanelUC3.Visible = true;
            }
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            if (UpdatePanelMainMenu.Visible == true && PanelUC1.Visible == true)
            {
                UpdatePanelMainMenu.Visible = false;
                PanelUC1.Visible = false;
                PanelUC4.Visible = true;
            }
        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            if (UpdatePanelMainMenu.Visible == true)
            {
                UpdatePanelMainMenu.Visible = false;
                PanelUC1.Visible = false;
                PanelUC5.Visible = true;
            }
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            if (UpdatePanelMainMenu.Visible == true)
            {
                UpdatePanelMainMenu.Visible = false;
                UpdatePanelDefault.Visible = true;
            }
        }

        protected void btn6_Click(object sender, EventArgs e)
        {

        }

        protected void num1_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "1";
                txtRePIN.Text += "1";
            }
        }

        protected void num2_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "2";
                txtRePIN.Text += "2";
            }
        }

        protected void num3_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "3";
                txtRePIN.Text += "3";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void num4_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "4";
                txtRePIN.Text += "4";
            }
        }

        protected void num5_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "5";
                txtRePIN.Text += "5";
            }
        }

        protected void num6_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "6";
                txtRePIN.Text += "6";
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {

        }

        protected void num7_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "7";
                txtRePIN.Text += "7";
            }
        }

        protected void num8_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "8";
                txtRePIN.Text += "8";
            }
        }

        protected void num9_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "9";
                txtRePIN.Text += "9";
            }
        }

        protected void num0_Click(object sender, EventArgs e)
        {
            if (UpdatePanelValidPIN.Visible == true || UpdatePanelReValidPIN.Visible == true)
            {
                txtPIN.Text += "0";
                txtRePIN.Text += "0";
            }
        }
    }
}